﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon; 
using AIPASoft.Interfaces;
using AIPASoft.Interfaces.BusinessDomain;
using AIPASoft.Interfaces.Security;
using AIPASoft.SecurityLibrary; 

namespace AIPASoftApplicationManager
{
    public class ApplicationManager : IApplicationManager
    {
        #region Implementation of IApplicationManager

        public ISecurityLibrary SecurityLibraryInstance { get; set; }
        public ILogProvider LogProviderInstance { get; set; }
        public IDomainManager DomainManagerInstance { get; set; }

        public Ribbon RibbonControl { get; set; } 
        public ContentControl ContentHolder { get; set; }

        private IUserRealm _userRealm;
        public IUserRealm CurrentUser
        {
            get { return _userRealm; }
            set
            {
                if (value == null)
                    OnUserLoggedOff();
                else
                {
                    _userRealm = value;
                    OnUserLoggedIn(value);
                }
            }
        }

        public ApplicationManager()
        { 
            SecurityLibraryInstance = new SecurityLibrary();
            DomainManagerInstance = new DomainManager.DomainManager(); 
        }

        
        private RibbonApplicationMenu _applicationMenu = new RibbonApplicationMenu();
        private void CreateApplicationMenuHolder()
        {
            _applicationMenu = new RibbonApplicationMenu()
            {
                //TODO: Move the strings to configurations/settings
                ToolTipDescription = "AIPASoft - Intellectual Property Management Application",
                ToolTipTitle = "AIPASoft 3.0"
            };
        }

        private void LoadApplicationDomainModels()
        { 

        } 
       
        public void Start()
        {
            if (RibbonControl == null || ContentHolder == null)
                return;

            CreateApplicationMenuHolder();
            LoadSecurityModels();
            LoadApplicationDomainModels();
            LoadDomains();
        }

        public void Finish()
        {
            UnLoadDomains();
        }

        public event LoadDomains LoadDomainsEvent;
        public event UnLoadDomains UnLoadDomainssEvent;

        #endregion


        #region Private Methods

        private List<Assembly> Plugins { get; set; }
        public List<IBusinessDomain> BusinessDomains { get; set; }
        
        private void LoadSecurityModels()
        {
            try
            {
                var menuItems = SecurityLibraryInstance.GetMenuItems();

                if (menuItems == null)
                    return;

                if (RibbonControl == null) return;

                foreach (var ribbonApplicationMenuItem in menuItems)
                {
                    _applicationMenu.Items.Add(ribbonApplicationMenuItem);
                }

                RibbonControl.ApplicationMenu = _applicationMenu;
            }
            catch (Exception exception)
            {
                //TODO: Implement Logging
                throw;
            }
        }
        
        private void LoadDomains()
        {
            var pluginHelper = new PluginHelper(this);
            BusinessDomains = pluginHelper.BusinessDomains;

            if (BusinessDomains == null)
                return;

            foreach (var businessDomain in BusinessDomains)
            {
                var tab = businessDomain.GetDomainTab();
                if (tab != null)
                    RibbonControl.Items.Add(tab);
            }
        }


        private void UnLoadDomains()
        {
            Plugins = null;
        }

        #endregion


        #region User Login Events

        public event UserLoggedIn UserLoggedInEvent;
        public event UserLoggedOff UserLoggedOffEvent;

        private void OnUserLoggedIn(IUserRealm userRealm)
        {
            if (UserLoggedInEvent != null)
                UserLoggedInEvent(userRealm);
        }

        private void OnUserLoggedOff()
        {
            if (UserLoggedOffEvent != null)
                UserLoggedOffEvent();
        }

        #endregion
    }
}
